package com.kotlin.novel.adapter


import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup

import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.kotlin.novel.R

import com.kotlin.novel.listener.OnItemViewClickListener

import com.kotlin.novel.model.TypeModel


class TypesAdapter(var context: Context, var typeModels: ArrayList<TypeModel>) : RecyclerView.Adapter<TypesAdapter.ViewHolder>() {

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var tvBookCount: TextView = itemView.findViewById(R.id.tv_item_bookCount)
        var tvMonthlyCount: TextView = itemView.findViewById(R.id.tv_item_monthlyCount)
        var tvType: TextView = itemView.findViewById(R.id.tv_item_type)

    }

    lateinit var onItemViewClickListener: OnItemViewClickListener<TypeModel>

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_type, parent, false))
    }

    override fun getItemCount(): Int {
        return typeModels.size
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val typeModel = typeModels[position]
        holder.tvBookCount.text = "数量 :"+typeModel.bookCount+"本"
        holder.tvMonthlyCount.text = "本月新增：" + typeModel.monthlyCount+"本"
        holder.tvType.text = typeModel.name
        holder.tvType.setOnClickListener { v -> v?.let { onItemViewClickListener.itemClick(it, typeModel) } }

    }


}